package com.lanou.service.impl;

import com.lanou.bean.LayUiTable;
import com.lanou.bean.Permission;
import com.lanou.bean.PermissionExample;
import com.lanou.bean.RolepmsExample;
import com.lanou.mapper.PermissionMapper;
import com.lanou.mapper.RolepmsMapper;
import com.lanou.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {
    private LayUiTable<Permission> layUiTable = null;

    private List<Permission> permission = null;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RolepmsMapper rolepmsMapper;
    @Override
    public int insertService(Permission permission) {
        int insert = permissionMapper.insert(permission);
        return insert;
    }

    @Override
    public int delectPermission(Integer pid) {
        RolepmsExample rolepmsExample= new RolepmsExample();
        rolepmsExample.createCriteria().andPIdEqualTo(pid);
        rolepmsMapper.deleteByExample(rolepmsExample);
        PermissionExample permissionExample = new PermissionExample();
        PermissionExample.Criteria criteria = permissionExample.createCriteria();
        if (pid != null){
            criteria.andPIdEqualTo(pid);
        }
        return permissionMapper.deleteByExample(permissionExample);
    }

    @Override
    public List<Permission> selectPermisson(Permission pms) {
        PermissionExample permissionExample = new PermissionExample();
        PermissionExample.Criteria criteria = permissionExample.createCriteria();
        List<Permission> permissions = permissionMapper.selectByExample(null);
        return permissions;
    }

    @Override
    public List<Permission> selectPermissons() {
        List<Permission> permissions = permissionMapper.selectByExample(null);
        return permissions;
    }

    @Override
    public LayUiTable<Permission> queryBooksForLayUI(Permission condition, Integer pageSize, Integer currentPage) {

            permission = selectPermisson(null);

        int statsIdx = (currentPage- 1) * pageSize;
        int endIdx = currentPage * pageSize;
        if (endIdx > permission.size()){
            endIdx = permission.size();
        }
        List<Permission> permissions = permission.subList(statsIdx, endIdx);
        LayUiTable<Permission> layUiTable = new LayUiTable<>();
        layUiTable.setCode(0);
        layUiTable.setCount((long)(permission.size()));
        layUiTable.setMsg("查询成功");
        layUiTable.setData(permissions);
        return layUiTable;
    }
    @Override
    public List<Permission> findAllP() {
        List<Permission> permissions = permissionMapper.selectByExample(null);
        return permissions;
    }
}
